#!/usr/bin/env bash
set -eo pipefail
IFS=$'\n\t'

if ! [ -x "$(command -v docker)" ]; then
  echo 'Error: docker is not installed.' >&2
  exit 1
fi

if ! [ -x "$(command -v docker-compose)" ]; then
  echo 'Error: docker-compose is not installed.' >&2
  exit 1
fi

sandbox () {
      if [ "$2" == "-nightly" ]; then
        source .env-nightlies
        echo "Using nightlies...removing old ones."
        # If nightly images already exist, containers are stopped, destroyed,
        # and rebuilt using newly pulled images
        if [ $(docker images | grep nightly | tr -s ' ' | cut -d ' ' -f 3 | wc -l) -gt 0 ]; then
          docker-compose down
          docker-compose rm -f
          docker-compose build --pull
        fi
      else
        source .env-latest
        echo "Using latest, stable releases"
      fi
  # Enter attaches users to a shell in the desired container
  enter () {
    case $2 in
      influxdb)
        echo "Entering /bin/bash session in the influxdb container..."
        docker-compose exec influxdb /bin/bash
        ;;
      chronograf)
        echo "Entering /bin/sh session in the chronograf container..."
        docker-compose exec chronograf /bin/sh
        ;;
      kapacitor)
        echo "Entering /bin/bash session in the kapacitor container..."
        docker-compose exec kapacitor /bin/bash
        ;;
      telegraf)
        echo "Entering /bin/bash session in the telegraf container..."
        docker-compose exec telegraf /bin/bash
        ;;
      *)
        echo "sandbox enter (influxdb||chronograf||kapacitor||telegraf)"
        ;;
    esac
  }

  # Logs streams the logs from the container to the shell
  logs () {
    case $2 in
      influxdb)
        echo "Following the logs from the influxdb container..."
        docker-compose logs -f influxdb
        ;;
      chronograf)
        echo "Following the logs from the chronograf container..."
        docker-compose logs -f chronograf
        ;;
      kapacitor)
        echo "Following the logs from the kapacitor container..."
        docker-compose logs -f kapacitor
        ;;
      telegraf)
        echo "Following the logs from the telegraf container..."
        docker-compose logs -f telegraf
        ;;
      *)
        echo "sandbox logs (influxdb||chronograf||kapacitor||telegraf)"
        ;;
    esac
  }

  case $1 in
    up)
      echo "Spinning up Docker Images..."
      echo "If this is your first time starting sandbox this might take a minute..."
      docker-compose up -d --build
      echo "Opening tabs in browser..."
      sleep 3
      if [ $(uname) == "Darwin" ]; then
        open http://localhost:3010
        open http://localhost:8888
      elif [ $(uname) == "Linux" ]; then
        xdg-open http://localhost:8888
        xdg-open http://localhost:3010
      else
        echo "no browser detected..."
      fi
      ;;
    down)
      echo "Stopping sandbox containers..."
      docker-compose down
      ;;
    restart)
      echo "Stopping all sandbox processes..."
      docker-compose down > /dev/null 2>&1
      echo "Starting all sandbox processes..."
      docker-compose up -d --build > /dev/null 2>&1
      echo "Services available!"
      ;;
    delete-data)
      echo "deleting all influxdb, kapacitor and chronograf data..."
      rm -rf kapacitor/data influxdb/data chronograf/data
      ;;
    docker-clean)
      echo "Stopping and removing running sandbox containers..."
      docker-compose down
      echo "Removing TICK images..."
      docker rmi sandbox_documentation influxdb:latest telegraf:latest kapacitor:latest chronograf:latest chrono_config:latest quay.io/influxdb/influxdb:nightly quay.io/influxdb/chronograf:nightly> /dev/null 2>&1
      docker rmi $(docker images -f "dangling=true" -q)
      ;;
    influxdb)
      echo "Entering the influx cli..."
      docker-compose exec influxdb /usr/bin/influx
      ;;
    flux)
      echo "Entering the flux repl..."
      docker-compose exec influxdb /usr/bin/influx -type flux
      ;;
    rebuild-docs)
      echo "Rebuilding documentation container..."
      docker build -t sandbox_documentation documentation/ > /dev/null 2>&1
      echo "Restarting..."
      docker-compose down > /dev/null 2>&1
      docker-compose up -d --build > /dev/null 2>&1
      ;;
    enter)
      enter $@
      ;;
    logs)
      logs $@
      ;;
    *)
      cat <<-EOF
sandbox commands:
  up (-nightly)      -> spin up the sandbox environment (latest or nightlies specified in the companion file)
  down               -> tear down the sandbox environment (latest or nightlies specified in the companion file)
  restart (-nightly) -> restart the sandbox
  influxdb           -> attach to the influx cli
  flux               -> attach to the flux REPL

  enter (influxdb||kapacitor||chronograf||telegraf) -> enter the specified container
  logs  (influxdb||kapacitor||chronograf||telegraf) -> stream logs for the specified container

  delete-data  -> delete all data created by the TICK Stack
  docker-clean -> stop and remove all running docker containers and images
  rebuild-docs -> rebuild the documentation image
EOF
      ;;
  esac
}

pushd `dirname $0` > /dev/null
    sandbox $@
popd > /dev/null
